package com.scitc.pleasurevideo.okgoutil;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.data.DataUtil;
import com.scitc.pleasurevideo.data.ETMan;
import com.scitc.pleasurevideo.data.StaticDataMan;
import com.scitc.pleasurevideo.pojo.VideoSourceVo;
import com.scitc.pleasurevideo.x5webview.MyApplication;
import com.tataera.stat.util.AndroidUtils;

public class OkGoUtil {
    private static OkGoUtil okGoUtil;

    public static synchronized OkGoUtil init(){
        if(okGoUtil==null){
            okGoUtil=new OkGoUtil();
        }
        return okGoUtil;
    }

    /**
     * 判断是不是该启动app
     * @param httpCompleteOrFail
     */
    public void isAppStart(HttpCompleteOrFail httpCompleteOrFail){
        String url= StaticDataMan.getMyurl+"isStartApp?";
        HttpParams params=new HttpParams();
        params.put("version", DataUtil.getVersionName(MyApplication.getInstance()));
        params.put("name", MyApplication.getInstance().getResources().getString(R.string.app_name_en));
        toApiGet(url,params,httpCompleteOrFail);
    }


    /**
     * 我的界面-登录界面
     * 登录
     * @param userId
     * @param psw
     * @param httpCompleteOrFail
     */
    public void toLogin(String userId,String psw,HttpCompleteOrFail httpCompleteOrFail){
        String url= StaticDataMan.getMyurl+"login?";
        HttpParams params=new HttpParams();
        params.put("userid",userId);
        params.put("userpsw",psw);
        toApiPost(url,params,httpCompleteOrFail);
    }

    /**
     * 我的界面-设置界面
     * 添加影视网址
     * @param videoSourceVo
     * @param httpCompleteOrFail
     */
    public void toaddVidoeSource(VideoSourceVo videoSourceVo,HttpCompleteOrFail httpCompleteOrFail){
        if(videoSourceVo==null){
            return;
        }
        String json= ETMan.getMananger().getGson().toJson(videoSourceVo);
        String url=StaticDataMan.getMyurl+"insertVideoSource?";
        HttpParams params=new HttpParams();
        params.put("json",json);
        toApiGet(url,params,httpCompleteOrFail);
    }

    /**
     * 我的界面-设置界面
     * 获取所有影视网址的接口
     * @param httpCompleteOrFail
     */
    public void getVideoSourceList(HttpCompleteOrFail httpCompleteOrFail){
        String url=StaticDataMan.getMyurl+"selectAllVideoSource";
        toApiGet(url,httpCompleteOrFail);
    }


    /**
     * 主页-首页
     * 获取所有影视的图标
     * @param httpCompleteOrFail
     */
    public void getVideoIconList(HttpCompleteOrFail httpCompleteOrFail){
        String url=StaticDataMan.getMyurl+"selectAllVideoIcon";
        toApiGet(url,httpCompleteOrFail);
    }

    /**
     * 我的界面-设置界面
     * 获取所有影视网址的接口（管理员）
     * @param httpCompleteOrFail
     */
    public void getVideoSourceListUser(HttpCompleteOrFail httpCompleteOrFail){
        String url=StaticDataMan.getMyurl+"selectAllVideoSource";
        toApiGet(url,httpCompleteOrFail);
    }


    private void toApiPost(String url, HttpParams params,HttpCompleteOrFail httpCompleteOrFail){
        if (!AndroidUtils.isNetworkConnected(MyApplication.getInstance())) {
            return;
        }
        OkGo.<String>post(url)
                .tag(MyApplication.getInstance())
                .params(params)
                .isMultipart(true)
                .execute(new StringCallback(){

                    @Override
                    public void onSuccess(Response<String> response) {
                        if("false".equals(response.body())){
                            httpCompleteOrFail.onFile("请求失败");
                        }else {
                            httpCompleteOrFail.onComplete(response.body());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                       httpCompleteOrFail.onFile("请求失败");
                    }
                });

    }
    private void toApiPost(String url,HttpCompleteOrFail httpCompleteOrFail){
        if (!AndroidUtils.isNetworkConnected(MyApplication.getInstance())) {
            return;
        }
        OkGo.<String>post(url)
                .tag(MyApplication.getInstance())
                .isMultipart(true)
                .execute(new StringCallback(){

                    @Override
                    public void onSuccess(Response<String> response) {
                        if("false".equals(response.body())){
                            httpCompleteOrFail.onFile("请求失败");
                        }else {
                            httpCompleteOrFail.onComplete(response.body());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                       httpCompleteOrFail.onFile("请求失败");
                    }
                });

    }
    private void toApiGet(String url, HttpParams params,HttpCompleteOrFail httpCompleteOrFail){
        if (!AndroidUtils.isNetworkConnected(MyApplication.getInstance())) {
            return;
        }
         OkGo.<String>get(url)
                .tag(MyApplication.getInstance())
                .params(params)
                .execute(new StringCallback(){
                    @Override
                    public void onSuccess(Response<String> response) {
                        if("false".equals(response.body())){
                            httpCompleteOrFail.onFile("请求失败");
                        }else {
                            httpCompleteOrFail.onComplete(response.body());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                       httpCompleteOrFail.onFile("请求失败");
                    }
                });
    }
    private void toApiGet(String url,HttpCompleteOrFail httpCompleteOrFail){
        if (!AndroidUtils.isNetworkConnected(MyApplication.getInstance())) {
            return;
        }
         OkGo.<String>get(url)
                .tag(MyApplication.getInstance())
                .execute(new StringCallback(){
                    @Override
                    public void onSuccess(Response<String> response) {
                        if("false".equals(response.body())){
                            httpCompleteOrFail.onFile("请求失败");
                        }else {
                            httpCompleteOrFail.onComplete(response.body());
                        }
                    }

                    @Override
                    public void onError(Response<String> response) {
                       httpCompleteOrFail.onFile("请求失败");
                    }
                });
    }
}
